﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class SoundManager : MonoSingleton<SoundManager>
{    /// <summary>
    /// 音效是否开启
    /// </summary>
    public bool SoundOn { get; private set; }
    /// <summary>
    /// 音乐是否开启
    /// </summary>
    public bool MusicOn { get; private set; }
    public System.Action<bool> OnMusicStateChange;
    /// <summary>
    /// 音效AudioSource
    /// </summary>
    private AudioSource _soundSource;
    /// <summary>
    /// 音乐AudioSource
    /// </summary>
    private AudioSource _musicSource;

    public override void Initialize()
    {
        _soundSource = InstantiateSource("_soundSource");
        _musicSource = InstantiateSource("_musicSource");
        LoadSoundState();
        LoadMusicState();
    }

    public void PlayOneShot(AudioClip clip)
    {
        if (!SoundOn)
        {
            return;
        }
        _soundSource.PlayOneShot(clip);
    }
    public void PlayOneShot(string name, int priority = 1)
    {
        if (string.IsNullOrEmpty(name))
        {
            return;
        }
        if (!SoundOn)
        {
            return;
        }
        AudioClip clip = Resources.Load<AudioClip>(ResourcesPath.GetSound(name));
        _soundSource.PlayOneShot(clip);
    }

    public void PlayMusic(AudioClip clip)
    {        
        if (_musicSource.clip != null)
        {
            _musicSource.clip.UnloadAudioData();
        }
        _musicSource.loop = true;
        _musicSource.clip = clip;
        _musicSource.Play();
    }
    public void PlayMusic(string name)
    {        
        if (string.IsNullOrEmpty(name) || ( _musicSource.clip != null && _musicSource.clip.name == name))
        {
            return;
        }
        AudioClip clip = Resources.Load<AudioClip>(ResourcesPath.GetSound(name));
        PlayMusic(clip);
    }

    public void SetSoundState(bool isOn)
    {
        SoundOn = isOn;
        SetSoundSourceState();
        SaveSoundState();
    }
    public void SetMusicState(bool isOn)
    {
        MusicOn = isOn;
        SetMusicSourceState();
        SaveMusicState();
        OnMusicStateChange?.Invoke(isOn);
    }

    private void SetSoundSourceState()
    {
        _soundSource.mute = !SoundOn;
    }
    private void SetMusicSourceState()
    {
        _musicSource.mute = !MusicOn;
    }
    private AudioSource InstantiateSource(string str)
    {
        GameObject go = new GameObject(str);
        go.transform.SetParent(this.transform);
        return go.AddComponent<AudioSource>();
    }
#region local data
    private const string key_soundstate = "key_soundstate";
    private const string key_musicstate = "key_musicstate";
    private void LoadSoundState()
    {
        SoundOn = PlayerPrefs.GetInt(key_soundstate, 1) == 1;
        SetSoundSourceState();
    }
    private void SaveSoundState()
    {
        PlayerPrefs.SetInt(key_soundstate, SoundOn ? 1 : 0);
    }
    private void LoadMusicState()
    {
        MusicOn = PlayerPrefs.GetInt(key_musicstate, 1) == 1;
        SetMusicSourceState();
    }
    private void SaveMusicState()
    {
        PlayerPrefs.SetInt(key_musicstate, MusicOn ? 1 : 0);
    }
#endregion
}

